package com.cmict.data.form.api.util;


import org.mapstruct.ap.shaded.freemarker.template.Configuration;
import org.mapstruct.ap.shaded.freemarker.template.Template;
import org.mapstruct.ap.shaded.freemarker.template.TemplateException;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;

/**
 * @description: sql拼接模板
 * @author: qiaoyu
 * @create: 2022-03-19 22:57
 **/
public class FreemarkerUtil {
	/**
	 * 通过指定的名字获取相应的模板
	 * @param
	 * @return
	 */
	public String getTemplate(Map<String, Object> map,String fltName) {
		try {
			Configuration cfg = new Configuration();
			cfg.setOutputEncoding("UTF-8");
			cfg.setDefaultEncoding("UTF-8");// 编码设置1
			cfg.setEncoding(Locale.CHINA, "UTF-8");
			cfg.setClassForTemplateLoading(this.getClass(), "/template");     //读取src目录下
			Template template = cfg.getTemplate(fltName);
			template.setEncoding("UTF-8");// 编码设置2
			StringWriter out = new StringWriter();
			template.process(map, out);
			System.out.println(out.getBuffer().toString());
			return out.getBuffer().toString();
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		}
		return null;
	}
}

